package com.zhongyouyun.admin.controller;


import cn.hutool.core.lang.tree.Tree;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.model.RoleModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.pojo.MenuPojo;
import com.zhongyouyun.admin.pojo.RoleMenuPojo;
import com.zhongyouyun.other.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.RoleMenuService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: shenyi
 * @Date: 2021-09-23
 * @Description: 角色资源中间表
 */
@RestController
@RequestMapping("/admin/z_role_menu")
@Api(value = "用户角色菜单controller", tags = {"用户角色菜单接口"})
public class RoleMenuController {
    @Autowired
    private RoleMenuService service;

    @PostMapping("/findRoleMenuTree")
    @ResponseBody
    @ApiOperation(value = "获取用户角色菜单", notes = "通过用户信息获取用户角色权限")
    public Result findRoleMenuTree(@RequestBody UserModel userModel) {
        List<Tree<String>> list = service.selectRoleMenuList(userModel);
        if (list.size() > 0) {
            Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
            resultMap.put("list", list);
            return Result.builder().message("成功").status(0).data(resultMap).build();
        } else {
            return Result.builder().message("没有任何权限").status(1).build();
        }
    }

    @PostMapping("/findPermissions")
    @ResponseBody
    @ApiOperation(value = "获取角色权限菜单", notes = "通过获取角色权限菜单")
    public Result<List<RoleMenuPojo>> findPermissions(@RequestBody UserModel userModel) {
        List<RoleMenuPojo> list = service.findPermissions(userModel);
        if (list.size() > 0) {
            return Result.<List<RoleMenuPojo>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<RoleMenuPojo>>builder().message("没有任何权限").status(1).build();
        }

    }


}
